<?php
declare (strict_types = 0);
namespace app\controller;

use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\Cache;


//评价控制器
class Comments{

    //常用数据表内容 及常用字典
    var $dept=[];
    var $users =[];
    var $stars=array('1'=>'★','2'=>'★★','3'=>'★★★','4'=>'★★★★','5'=>'★★★★★');

    public function __construct(){

        $um=new UserModel();
        $this->users = $um->getAll();
        
        $dm=new DeptModel();
        $this->dept = $dm->getAll();
    }

    //类别
    public function index(){
        
        $cm=new Cat2Model();
        //$cats=$cm->getAll();
        $cats=$cm->catTree();

        foreach($cats as &$kv){
            $kv['fid_name'] = $this->goodsType[$kv['id']];                        
            $kv['status_name'] = $this->status[$kv['status']];
            $kv['creater_name'] = $this->users[$kv['creater']]['name'];
        }

        View::assign(['category'=>$this->goodsType,'data'=>$cats]);

        return View::fetch();
    }  


    //分类新增
    public function comm_edit(){
        $uid = Session::get('logonid');
        if(!$uid) return header('location:/index.php/login/customer_login');

        $all = Request::param();
        if(empty($all['order_id'])) return json_encode(['code'=>0,'msg'=>'Order not found!']);
        $order_id =$all['order_id'];

        
        $newRow=['id'=>'','order_id'=>$order_id,'stars'=>1,'content'=>'','create_time'=>''];
        //我的评价
        $row1 = Db::table('comments')->where('order_id',$order_id)->where('customer_id',$uid)->select()->toArray(); 
        if(!$row1){
            $row1=$newRow;
        }else{
            $row1=$row1[0];
            $row1['stars_name'] =$this->stars[$row1['stars']];
        }
        //默认
        $row2 = Db::table('comments')->where('order_id',$order_id)->where('customer_id','<>',$uid)->select()->toArray(); 
        if(!$row2){
            $row2=$newRow;
        }else{
            $row2=$row2[0];
            $row2['stars_name'] =$this->stars[$row2['stars']];
        }

        if($_POST){
            $msg_1='';
            if(!$all['content']) return json_encode(['code'=>0,'msg'=>'Invalid comment text!']);
            
            $all['create_time']  = date("Y-m-d H:i:s",time());
            $all['customer_id'] =$uid;
            $res =Db::table('comments')->save($all);

            if($all['stars']==5){
                $order = Db::table('order')->find($all['order_id']);
                if($uid==$order['creater']){
                    $sql = "update customer set credit=credit+5 where id=".$order['driver_id'];
                }
                if($uid==$order['driver_id']){
                    $sql = "update customer set credit=credit+5 where id=".$order['creater'];
                }
                Db::execute($sql);
            }
            return json_encode(['code'=>1,'msg'=>'评价成功！'.$msg_1?:'']);
        }

        //halt($row1,$row2);
        View::assign(['order_id'=>$order_id,'data1'=>$row1,'data2'=>$row2,'stars'=>$this->stars]);
        return View::fetch();
    }

  

}
